/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import models.Usuario;
import models.Carrito;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;


public class LoginFormController extends SimpleFormController {

    private Carrito carrito;

    public void setCarrito(Carrito carrito) {
        this.carrito = carrito;
    }

    @Override
    protected ModelAndView onSubmit(Object command) throws Exception {

        Usuario usuario = (Usuario) command; //comandname cliente

        ModelAndView view = new ModelAndView();

        if (usuario.getUsuarioId().equals("1-9") && usuario.getPassword().equals("123")) {
            view.setViewName(getSuccessView());
            view.addObject("productos", carrito.getProductos());
        } else {
            view.setViewName("errorLogin");
            view.addObject("error", "Usuario o clave Incorrecto");
        }

        return view;
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request)
            throws ServletException, Exception {
        return new Usuario();
    }

    @Override
    protected Map referenceData(HttpServletRequest request) throws Exception {
        Map datos = new HashMap();
        datos.put("titulo", "Inicio de Sesión");
        return datos;
    }
}
